package com.marcandjo.gwtproject.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.marcandjo.gwtproject.client.model.Category;
import com.marcandjo.gwtproject.client.model.Photo;
import com.marcandjo.gwtproject.client.model.PhotoModel;
import com.marcandjo.gwtproject.client.model.PhotoModelFactory;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class EPhotoViewer implements EntryPoint, ValueChangeHandler<String> {

	private Image image = new Image();
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() 
	{
		DockLayoutPanel p = new DockLayoutPanel(Unit.EM);
	    p.addNorth(createTitle(), 4);
	    p.addSouth(createStatus(), 4);
	    p.addWest(new NavigationBar(), 10);
	    p.add(createContent());

	    RootLayoutPanel rp = RootLayoutPanel.get();
	    rp.add(p);
		
		// Add history listener
	    History.addValueChangeHandler(this);
	    
	}
	
	private LayoutPanel createTitle()
	{
		LayoutPanel panel = new LayoutPanel();
		panel.setStyleName("appl-title");
		
		Label label = new Label("Title");
		label.addStyleName("appl-title");
		
		panel.add(label);
		
		return panel;
	}
	
	private LayoutPanel createStatus()
	{
		LayoutPanel panel = new LayoutPanel();
		panel.setStyleName("appl-status");
		
		Label label = new Label("Status bar");
		label.addStyleName("appl-status");

		panel.add(label);
		
		return panel;
	}
	
	private FlowPanel createContent()
	{
        FlowPanel fp = new HorizontalFlowPanel();

    	PhotoModel model = PhotoModelFactory.getInstance();
    	for (Category category : model.getModel())
    	{ 
    		for (Photo photo : category.getPhotos())
    		{
				fp.add(new PhotoWidget(photo));
    		}
    	}

        return fp;
	}

	public void onValueChange(ValueChangeEvent<String> event) {
		Window.alert("EPhotoViewer: " + event.getValue());

    	PhotoModel model = PhotoModelFactory.getInstance();
    	for (Category category : model.getModel())
    	{
    		for (Photo photo : category.getPhotos())
    		{
    			if (photo.getId().equals(event.getValue()))
    			{
    		    	image.setUrl(photo.getUrl());
    				return;
    			}
    		}
    	}
	}
	


}
